import 'package:coinbaby/common/const/cosnt.dart';
import 'package:coinbaby/common/textstyle/textstyle.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';

class PwdSettingPage extends StatefulWidget {
  @override
  _PwdSettingPageState createState() => _PwdSettingPageState();
}

class _PwdSettingPageState extends State<PwdSettingPage> {

  final _pwdController = TextEditingController();
  final _pwd2Controller = TextEditingController();
  final _pwdFocus = FocusNode();
  final _pwd2Focus = FocusNode();

  @override
  void dispose() {
    _pwdController.dispose();
    _pwd2Controller.dispose();
    super.dispose();
  }

  void _login() {
    _jumpToRoot();
  }

  void _jumpToRoot(){
    SharedPreferences.getInstance().then((value) => value.setBool(AppConst.APP_IS_LOGIN, true));
    Navigator.of(context).pushNamedAndRemoveUntil('/root',(Route<dynamic> route) => false);
  }

  void _unfocus() {
    _pwdFocus.unfocus();
    _pwd2Focus.unfocus();
  }

  @override
  Widget build(BuildContext context) {

    final size = MediaQuery.of(context).size;
    final marginH = size.height * 0.06;

    return Scaffold(
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: _unfocus,
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.keyboard_arrow_left),
                onPressed: () => Navigator.pop(context),
              ),
              SizedBox(height: marginH,),
              Padding(
                padding: const EdgeInsets.all(20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text("请设置密码", style: AppFont.textStyle(26, Colors.black, fontWeight: FontWeight.bold),),
                    SizedBox(height: 10),
                    Text("8-20个字符，不可以是纯数字", style: AppFont.textStyle(14, Colors.grey),),
                    SizedBox(height: marginH,),
                    TextField(
                        maxLength: 20,
                        controller: _pwdController,
                        focusNode: _pwdFocus,
                        obscureText: true,
                        decoration: InputDecoration(
                          hintText: "请输入密码",
                          counterText: "",
                          hintStyle: AppFont.textStyle(14, Colors.grey[300]),
                          labelStyle: AppFont.textStyle(14, Colors.black)
                        ),
                      ),
                      SizedBox(height: 18,),
                      TextField(
                        maxLength: 6,
                        controller: _pwd2Controller,
                        focusNode: _pwd2Focus,
                        inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
                        keyboardType: TextInputType.number,
                        decoration: InputDecoration(
                          hintText: "请输入邀请码（必填）",
                          hintStyle: AppFont.textStyle(14, Colors.grey[300]),
                          labelStyle: AppFont.textStyle(14, Colors.black),
                          counterText: ""
                        ),
                      ),

                      InkWell(
                        onTap: _login,
                        child: Container(
                          width: size.width,
                          height: 50,
                          margin: EdgeInsets.only(top: marginH),
                          decoration: BoxDecoration(
                            color: Colors.red,
                            borderRadius: BorderRadius.circular(10)
                          ),
                          alignment: Alignment.center,
                          child: Text("登陆", style: AppFont.textStyle(14, Colors.white),),
                        ),
                      ),
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}